/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VistaGestionEmpleados.java
 *
 * Created on 11-nov-2010, 12:17:07
 */

package panaderialabaguette.vista;

import java.awt.Toolkit;
import javax.swing.DefaultCellEditor;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import panaderialabaguette.Panaderia;


/**
 *
 * @author ESPINOZA-MORENO
 */
public class VistaGestionEmpleados extends javax.swing.JFrame {

    private Panaderia panaderia;
    public static VistaGestionEmpleados vistaGestionEmpleados;
    /**
     * Dibuja el menu correspondiente a la gestion de empleados
     */
    public VistaGestionEmpleados(Panaderia panaderia) {
        this.setPanaderia(panaderia);
        initComponents();
        this.setIconImage(Toolkit.getDefaultToolkit().getImage(ClassLoader.getSystemResource("imagenes/empleados2.png")));
        this.setLocationRelativeTo(null);
        this.cargarUsuarios();
    }

     /**
     * Diseña un modelo de lista desplegable con los datos del usuario
     */
    public void cargarUsuarios(){
        DefaultComboBoxModel modelo;
        modelo = new DefaultComboBoxModel();
        this.jComboBox1.setModel(this.panaderia.obtenerUsuarios(modelo));
    }


    /**
     * Obtiene todos los datos de todos los empleados
     * @return una tabla con los datos de los empleados
     */
    private void mostrarEmpleado(){

        DefaultTableModel datos = (DefaultTableModel) this.jTable1.getModel();
        
        try {
            datos.removeRow(0);
            this.jTable1.setModel(this.panaderia.obtenerEmpleado(datos,jComboBox1.getSelectedItem().toString()));
            this.jTable1.setEnabled(true);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this,"Hubo un problema al obtener todos los empleados..."+ex.getMessage(),"ERROR",2);
        }
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jComboBox2 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        botonModificar = new javax.swing.JButton();
        botonEliminar = new javax.swing.JButton();
        botonCerrar = new javax.swing.JButton();

        jComboBox2.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "gerente", "administrador", "empleado de ventas" }));

        setTitle("Empleados");
        setAlwaysOnTop(true);
        setBounds(new java.awt.Rectangle(250, 150, 0, 0));
        setIconImage(getIconImage());
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setForeground(new java.awt.Color(0, 0, 102));
        jLabel2.setText("Seleccione el nombre del empleado que desea modificar o eliminar");
        jLabel2.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null, null}
            },
            new String [] {
                "D.N.I.", "Nombre", "Función", "Calle", "Número", "Depto.", "Piso", "Teléfono"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true, true, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTable1.setEnabled(false);
        jScrollPane1.setViewportView(jTable1);
        TableColumn tableColumn= jTable1.getColumnModel().getColumn(2);
        tableColumn.setCellEditor(new DefaultCellEditor(jComboBox2));

        botonModificar.setText("Modificar");
        botonModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonModificarActionPerformed(evt);
            }
        });

        botonEliminar.setText("Eliminar");
        botonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonEliminarActionPerformed(evt);
            }
        });

        botonCerrar.setText("Cerrar");
        botonCerrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botonCerrarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(botonModificar, javax.swing.GroupLayout.PREFERRED_SIZE, 95, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(botonCerrar, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 689, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 410, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 17, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(botonCerrar)
                    .addComponent(botonEliminar)
                    .addComponent(botonModificar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

  
    private void botonCerrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonCerrarActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
        DefaultTableModel datos = (DefaultTableModel) this.jTable1.getModel();
        datos.removeRow(datos.getRowCount()-1);
        String [] data = {"","","","","","","",""};
        datos.addRow(data);
        this.jTable1.setEnabled(false);
        Panaderia.setHayOperacionEnCurso(false);
    }//GEN-LAST:event_botonCerrarActionPerformed
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        // TODO add your handling code here:
        this.mostrarEmpleado();
    }//GEN-LAST:event_jComboBox1ActionPerformed
    private void botonModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonModificarActionPerformed
        // TODO add your handling code here:
        if (this.jTable1.getSelectedColumn() != 2)
            this.jTable1.getCellEditor().stopCellEditing();
        this.panaderia.modificarEmpleado(this);
    }//GEN-LAST:event_botonModificarActionPerformed

    /**
     * Elimina al empleado de la base de datos
     * @param evt
     */
    private void botonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botonEliminarActionPerformed
        // TODO add your handling code here:
        this.panaderia.eliminarEmpleado(this);
    }//GEN-LAST:event_botonEliminarActionPerformed

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        // TODO add your handling code here:
        DefaultTableModel datos = (DefaultTableModel) this.jTable1.getModel();
        datos.removeRow(datos.getRowCount()-1);
        String [] data = {"","","","","","","",""};
        datos.addRow(data);
        this.jTable1.setEnabled(false);
        Panaderia.setHayOperacionEnCurso(false);
    }//GEN-LAST:event_formWindowClosing

    public Panaderia getPanaderia() {
        return panaderia;
    }

    public void setPanaderia(Panaderia panaderia) {
        this.panaderia = panaderia;
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botonCerrar;
    private javax.swing.JButton botonEliminar;
    private javax.swing.JButton botonModificar;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JComboBox jComboBox2;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    public javax.swing.JTable jTable1;
    // End of variables declaration//GEN-END:variables

}
